<!--  -->
<template>
  <div class="layui-container fly-marginTop">
    <div class="fly-panel fly-panel-user" pad20>
      <div class="layui-tab layui-tab-brief" lay-filter="user">
        <ul class="layui-tab-title">
          <li>
            <router-link :to="{ name: 'login' }">登录</router-link>
          </li>
          <li class="layui-this">重置</li>
          <!-- <li>
                <router-link class="layui-this" :to="{name: 'reg'}">重置</router-link>
              </li> -->
        </ul>
        <div
          class="layui-form layui-tab-content"
          id="LAY_ucm"
          style="padding: 20px 0"
        >
          <validation-observer ref="observer" v-slot="{ validate }">
            <div class="layui-tab-item layui-show">
              <!-- {{# if(d.getPass === 1){}} -->
              <div class="fly-msg">请重置{{ username }}密码</div>
              <div class="layui-form layui-form-pane" style="margin-top: 30px">
                <form action="/user/repass" method="post">
                  <div class="layui-form-item">
                    <validation-provider
                      name="password"
                      rules="required|min:4|"
                      v-slot="{ errors }"
                      vid="confirmation"
                    >
                      <div class="layui-row">
                        <label for="L_pass" class="layui-form-label"
                          >密码</label
                        >
                        <div class="layui-input-inline">
                          <input
                            type="password"
                            name="password"
                            v-model="password"
                            placeholder="请输入密码"
                            autocomplete="off"
                            class="layui-input"
                          />
                        </div>
                        <div class="layui-form-mid layui-word-aux">
                          6到16个字符
                        </div>
                      </div>
                      <div class="layui-row">
                        <span style="color: #c00">{{ errors[0] }}</span>
                      </div>
                    </validation-provider>
                  </div>
                  <div class="layui-form-item">
                    <validation-provider
                      v-slot="{ errors }"
                      rules="required|confirmed:confirmation"
                    >
                      <div class="layui-row">
                        <label for="L_repass" class="layui-form-label"
                          >确认密码</label
                        >
                        <div class="layui-input-inline">
                          <input
                            type="password"
                            name="repassword"
                            v-model="repassword"
                            placeholder="请输入密码"
                            autocomplete="off"
                            class="layui-input"
                          />
                        </div>
                      </div>
                      <div class="layui-row">
                        <span style="color: #c00">{{ errors[0] }}</span>
                      </div>
                    </validation-provider>
                  </div>
                  <div class="layui-form-item">
                    <validation-provider
                      name="code"
                      ref="coderr"
                      rules="required|length:4"
                      v-slot="{ errors }"
                    >
                      <div class="layui-row">
                        <label for="L_vercode" class="layui-form-label"
                          >验证码</label
                        >
                        <div class="layui-input-inline">
                          <input
                            type="text"
                            name="code"
                            v-model="code"
                            placeholder="请输入验证码"
                            autocomplete="off"
                            class="layui-input"
                          />
                        </div>
                        <div class>
                          <span
                            class="svg"
                            style="color: #c00"
                            @click="_getCode()"
                            v-html="svg"
                          ></span>
                        </div>
                      </div>
                      <div class="layui-form-mid">
                        <span style="color: #c00">{{ errors[0] }}</span>
                      </div>
                    </validation-provider>
                  </div>
                  <div class="layui-form-item">
                    <!-- <input type="hidden" name="username" value="{{d.username}}">
                        <input type="hidden" name="email" value="{{d.email}}"> -->
                    <button
                      id="eventquery"
                      type="button"
                      class="layui-btn"
                      @click="validate().then(submit)"
                      @keyup.enter="validate().then(submit)"
                    >
                      提交
                    </button>
                  </div>
                </form>
              </div>
              <!-- {{# } else { }}
        
                  {{# if(d.getPass === 2){ }}
                  <div class="fly-error">该重置密码链接已失效，请重新校验您的信息</div>
                  {{# } else if(d.getPass === 3){ }}
                  <div class="fly-error">非法链接，请重新校验您的信息</div>
                  {{# } }} -->

              <!-- <div class="layui-form layui-form-pane">
                    <form method="post">
                      <div class="layui-form-item">
                        <label for="L_email" class="layui-form-label">邮箱</label>
                        <div class="layui-input-inline">
                          <input type="text" id="L_email" name="email" required lay-verify="required" autocomplete="off" class="layui-input">
                        </div>
                      </div>
                      <div class="layui-form-item">
                        <label for="L_vercode" class="layui-form-label">人类验证</label>
                        <div class="layui-input-inline">
                          <input type="text" id="L_vercode" name="vercode" required lay-verify="required" placeholder="请回答后面的问题" autocomplete="off" class="layui-input">
                        </div>
                        <div class="layui-form-mid">
                          <span style="color: #c00;"></span>
                        </div>
                      </div>
                      <div class="layui-form-item">
                        <button class="layui-btn" alert="1" lay-filter="*" lay-submit>提交</button>
                      </div>
                    </form>
                  </div> -->

              <!-- {{# } }} -->
            </div>
          </validation-observer>
        </div>
      </div>
    </div>
  </div>
</template>
<script>
import { ValidationProvider, ValidationObserver } from 'vee-validate'
import { resetInfo } from '@/api/login'
import mixin from '@/mixin/code'

export default {
  name: 'reset',
  mixins: [mixin],
  components: {
    ValidationProvider,
    ValidationObserver,
  },
  data () {
    return {
      svg: '',
      password: '',
      repassword: '',
      code: '',
      username: '',
      errorMsg: [],
      token: ''
    };
  },
  computed: {},
  mounted () {
    let queryStr = window.location.href.replace(/.*\?/, '')
    let obj = Object.fromEntries(queryStr.split('&').map(v => v.split('=')))
    this.token = obj.key
    this.username = decodeURIComponent(obj.username)
  },
  methods: {
    async submit () {
      const isValid = await this.$refs.observer.validate()
      if (!isValid) {
        // ABORT!!
        return
      }
      resetInfo({
        password: this.password,
        repassword: this.repassword,
        code: this.code,
        sid: localStorage.getItem('sid'),
        token: this.token
      }).then((res) => {
        if (res.code === 200) {
          this.$alert('重置密码成功')
          this.$router.push('/login')
        }
        if (res.code === 500) {
          this.$refs.coderr.setErrors([res.msg])
          this._getCode()
        }
        if (res.code === 404) {
          this.$alert(res.msg)
          this.$router.push('/forget')
        }
      })
    }
  }
}

</script>
<style scoped lang='scss' >
</style>